package com.thememaker.lockstyle;

import com.thememake.Constants;
import com.thememake.common.base.Strings;
import com.thememake.entity.ThemeFileInfo;
import com.thememake.util.FileUtils;
import com.thememake.util.image.ImageUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * Created by jiajun.jiang on 2018/3/9.
 */
public class StatusBarShowUtils {

    private ThemeFileInfo themeFileInfo;
    private static double scale = 1;

    public StatusBarShowUtils(ThemeFileInfo themeFileInfo,double width){
        this.themeFileInfo = themeFileInfo;
        scale = width/480d;
    }


    public Node getLauncherStatusBarView(String bgPathString) {
        HBox hBox = new HBox();
        hBox.setMinWidth(480*scale);
        hBox.setMinHeight(30*scale);
        hBox.setMaxHeight(30*scale);
        StackPane stackPane = new StackPane();
        stackPane.setMinWidth(480*scale);
        stackPane.setMinHeight(30*scale);
        stackPane.setMaxHeight(30*scale);

        HBox signalHbox = new HBox();
        signalHbox.setAlignment(Pos.CENTER_LEFT);
        signalHbox.setMinHeight(30*scale);
        signalHbox.setMaxHeight(30*scale);
        ImageView statusbarBg = getImageView("titlepreview" + File.separator + "vivo_statusbar.png", 480*scale, 49*scale);
        ImageView signal = getImageView("titlepreview" + File.separator + "signal_light.png", 17*scale, 14*scale);
        ImageView signalNumber = getImageView("com.android.systemui" + File.separator + "2.5" + File.separator + "480" + File.separator + "vivo_signal4_small_4_white.png", 30*scale, 14*scale);
        ImageView time = getImageView("titlepreview" + File.separator + "time_light.png", 47*scale, 14*scale);
        ImageView wifi = getImageView("titlepreview" + File.separator + "wifi_light.png", 18*scale, 14*scale);
//        ImageView battery=getImageView("com.android.systemui"+File.separator+"2.5"+File.separator+"480"+File.separator+"vivo_battery_20_19_white.png",32,13);
        ImageView battery = getImageView("com.android.systemui" + File.separator + "2.5" + File.separator + "480" + File.separator + "vivo_battery_20_19_white.png");


        //Label label = new Label("08:00");
        if (!Strings.isNullOrEmpty(bgPathString)){
            int[] colorBg = getImagePixel(bgPathString);
            //String textColor = "#000000";
            if (!ImageUtils.isShenRGB(colorBg)) {
                //  textColor = "#ffffff";
                signal = getImageView("titlepreview" + File.separator + "signal_black.png", 17, 14);
                signalNumber = getImageView("com.android.systemui" + File.separator + "2.5" + File.separator + "480" + File.separator + "vivo_signal4_small_4_color.png", 30, 14);
                time = getImageView("titlepreview" + File.separator + "time_black.png", 47, 14);
                wifi = getImageView("titlepreview" + File.separator + "wifi_black.png", 18, 14);
//             battery=getImageView("com.android.systemui"+File.separator+"2.5"+File.separator+"480"+File.separator+"vivo_battery_20_19_color.png",32,13);
                battery = getImageView("com.android.systemui" + File.separator + "2.5" + File.separator + "480" + File.separator + "vivo_battery_20_19_color.png");
            }
        }

        signalHbox.getChildren().addAll(signal, signalNumber);
        HBox hBox1 = new HBox();
        hBox1.setAlignment(Pos.CENTER_RIGHT);
        hBox1.setMinWidth(480*scale);
        hBox1.setMinHeight(30*scale);
        hBox1.setMaxHeight(30*scale);
        hBox1.getChildren().addAll(wifi, battery);
        HBox.setMargin(wifi, new Insets(0.0, 5.0*scale, 0.0, 0.0));
        HBox.setMargin(battery, new Insets(0.0, 10.0*scale, 0.0, 0.0));

        StackPane.setAlignment(signalHbox, Pos.CENTER_LEFT);
        StackPane.setMargin(signalHbox, new Insets(0.0, 0.0, 0.0, 10.0*scale));

        StackPane.setAlignment(time, Pos.CENTER);
        //   setTextColor(label, textColor, 16);
        hBox.setAlignment(Pos.CENTER);
        stackPane.getChildren().addAll(statusbarBg,signalHbox, hBox1);
        hBox.getChildren().addAll(stackPane);
        Group group = new Group();
        group.getChildren().addAll(hBox);
        return group;
    }

    public final ImageView getImageView(String relativePath, double width, double height) {
        ImageView image = new ImageView();
        try {
            File file = checkPath(themeFileInfo, new File(themeFileInfo.getOutputDir(), relativePath));
            System.out.println(file.getPath());
            if (file.exists()) {
                javafx.scene.image.Image img = null;
                img = new javafx.scene.image.Image(file.toURI().toURL().toString());
                image.setImage(img);
                image.setFitWidth(width);
                image.setFitHeight(height);
                //image.setSmooth(true);
                //image.setPreserveRatio(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    private ImageView getImageView(String relativePath) {
        ImageView image = new ImageView();
        try {
            File file = checkPath(themeFileInfo, new File(themeFileInfo.getOutputDir(), relativePath));
            System.out.println(file.getPath());
            if (file != null && file.exists()) {
                javafx.scene.image.Image img = null;
                img = new javafx.scene.image.Image(file.toURI().toURL().toString());
                image.setImage(img);
                image.setFitWidth(img.getWidth());
                image.setFitHeight(img.getHeight());
                image.setSmooth(true);
                //image.setPreserveRatio(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    public int[] getImagePixel(String image) {
        int[] rgb = new int[3];
        File file = new File(image);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int pixel = bi.getRGB(240, 10); // 下面三行代码将一个数字转换为RGB数字
        rgb[0] = (pixel & 0xff0000) >> 16;
        rgb[1] = (pixel & 0xff00) >> 8;
        rgb[2] = (pixel & 0xff);
        return rgb;
    }

    public File checkPath(ThemeFileInfo themeFileInfo, File bgFile) {
        try {

            if (bgFile.getPath().contains("output")) {
                if (!bgFile.exists()) {
                    String path = bgFile.getPath().replace("output", "pack\\themeZipImport");
                    if (new File(path).exists()) {
                        bgFile = new File(path);
                    } else {
                        String newPath = bgFile.getPath().replace(themeFileInfo.getOutputDir().getPath(),
                                FileUtils.getResFileOfThemeModel(themeFileInfo.getThemeType(), "default-preview").getPath());
                        bgFile = new File(newPath);
                    }
                }
            } else {
                if (!bgFile.exists()) {
                    String path = bgFile.getPath().replace("pack\\themeZipImport", "output");
                    if (new File(path).exists()) {
                        bgFile = new File(path);
                    } else {
                        String newPath = bgFile.getPath().replace(themeFileInfo.getOutputDir() + File.separator + "pack\\themeZipImport",
                                FileUtils.getResFileOfThemeModel(themeFileInfo.getThemeType(), "default-preview").getPath());
                        bgFile = new File(newPath);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bgFile;
    }
}
